package com.lx.video.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lx.bean.Result;
import com.lx.bilibili.entity.VideoAndVideoPlayHistory;
import com.lx.bilibili.entity.VideoPlayHistory;
import com.lx.video.biz.impl.VideoplayhistroyServiceImpl;
import com.lx.video.dao.VideoAndVideoplayhistroyMapper;
import com.lx.video.dao.VideoplayhistroyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2025-04-06
 */
@RestController
@RequestMapping("/videoplayhistroy")
public class VideoplayhistroyController {

    @Autowired
    VideoplayhistroyMapper videoplayhistroyMapper;

    @Autowired
    VideoplayhistroyServiceImpl videoplayhistroyService;


    @Autowired
    VideoAndVideoplayhistroyMapper videoAndVideoplayhistroyMapper;

    @RequestMapping("getvideodenTime")
    public VideoPlayHistory getvideoHis(int uid, int vid){
        LambdaQueryWrapper<VideoPlayHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(VideoPlayHistory::getUid, uid);
        lqw.eq(VideoPlayHistory::getVid, vid);
        lqw.select(VideoPlayHistory::getEndtime);
        return videoplayhistroyMapper.selectOne(lqw);
    }

    @RequestMapping("saveVideoProgress")
    public int saveVideoProgress(@RequestBody VideoPlayHistory videoplayhistroy){
        if(videoplayhistroy.getUid() == null){
            return 0;
        }
        videoplayhistroy.setTime(LocalDateTime.now());
        return videoplayhistroyService.saveVideoProgress(videoplayhistroy);
    }

    @RequestMapping("getVideoPlayHistory")
    public Result getVideoPlayHistory(int id){
        return new Result(1, "获取成功", videoAndVideoplayhistroyMapper.getVideoPlayHistory(id));
    }

    @RequestMapping("getVideoPlayHistoryByTime")
    public Result getVideoPlayHistoryByTime(int uid,
                                           @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)  LocalDateTime startTime,
                                           @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)  LocalDateTime endTime){

        return new Result(1, "获取成功", videoAndVideoplayhistroyMapper.getVideoPlayHistoryToday(uid, startTime, endTime));
    }

    @RequestMapping("deleteVideoPlayHistory")
    @Transactional
    public Result deleteVideoPlayHistory( @RequestBody List<Integer> vphid){
        for(int i=0;i<vphid.size();i++){
            videoplayhistroyMapper.deleteById(vphid.get(i));
        }
        return new Result(1, "删除成功", null);
    }

    @RequestMapping("deleteAllVideoPlayHistory")
    public Result deleteAllVideoPlayHistory(int id){
        LambdaUpdateWrapper<VideoPlayHistory> luw= new LambdaUpdateWrapper<>();
        luw.eq(VideoPlayHistory::getUid, id);
        int delete = videoplayhistroyMapper.delete(luw);
        if(delete > 0){
            return new Result(0, "删除失败", null);
        }
        return new Result(1, "删除成功", null);
    }

    @RequestMapping("getVideoPlayHistoryByText")
    public Result getVideoPlayHistoryByText(String text, int id){
        text = "%" + text + "%";
        return new Result(1, "获取成功", videoAndVideoplayhistroyMapper.getVideoPlayHistoryByText(text, id));
    }

}
